<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    :disabled="!addState"
                    @click="opAdd"
                >新增</el-button>
                <el-button
                    type="info"
                    plain
                    icon="el-icon-delete"
                    :disabled="!deleteState"
                    @click="opBatchDelete"
                >删除</el-button>
            </div>
            <div class="right-panel">
                <el-button :icon="Search"   type="primary" @click="opBindData">查询</el-button>
                <el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
                <el-button text type="primary" @click="showCondition = !showCondition">
                    <span v-if="!showCondition">展开</span>
                    <span v-else>收起</span>
                    <vab-icon
                        class="vab-dropdown"
                        :class="{ 'vab-dropdown-active': fold }"
                        icon="arrow-up-s-line"
                    />
                </el-button>
            </div>
        </el-header>
        <el-foot v-show="showCondition">
            <vab-query-form-top-panel>
                <el-form inline label-width="49px" :model="queryForm" style="margin-top: 5px;margin-right: 20px;">
                    <el-row type="flex" style="flex-wrap:wrap">
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="仓库编号" label-width="100px" style="width: 100%;">
                                <el-input
                                    placeholder="---请输入编号---"
                                    v-model="queryList.warehouseCode"
                                    style="width: 100%;"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="仓库名称" label-width="100px" style="width: 100%;">
                                <el-input
                                    placeholder="---请输入名称---"
                                    v-model="queryList.warehouseName"
                                    style="width: 100%;"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="仓库类型" label-width="100px" style="width: 100%;">
                                <el-select
                                    placeholder="---请选择状态---"
                                    v-model="queryList.warehouseType"
                                    style="width: 100%;"
                                >
                                    <el-option label="---请选择---" value />
                                    <el-option
                                        v-for="item in optionsWarehouseType"
                                        :key="item.value" 
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="外部托管仓" label-width="100px" style="width: 100%;">
                                <el-select
                                    placeholder="---请选择是否外部托管仓---"
                                    v-model="queryList.isExternal"
                                    style="width: 100%;"
                                >
                                    <el-option label="---请选择---" value />
                                    <el-option label="是" :value="true" />
                                    <el-option label="否" :value="false" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" >
                            <el-form-item label="状态" label-width="100px" style="width: 100%;">
                                <el-select
                                    placeholder="---请选择状态---"
                                    v-model="queryList.status"
                                    style="width: 100%;"
                                >
                                    <el-option label="---请选择---" value />
                                    <el-option
                                        v-for="item in optionsState"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    
                   
                    
                   
                    
                </el-form>
            </vab-query-form-top-panel>
        </el-foot>

        <el-main class="nopadding">
            <scTable
                ref="mytable"
                :queryType="'warehouse/pages'"
                :data="listData"
                :queryList="queryList"
                :column="columnList"
                :orderList="OrderList"
                :sort-orders="['ascending', 'descending']"
                @selection-change="selectionChange"
                row-key="warehouseId"
                key="warehouseId"
                stripe
                show-summary
                remoteSummary
            >
                <el-table-column type="selection" min-width="50"></el-table-column>
                <el-table-column prop="warehouseCode" label="仓库编号" min-width="120" sortable></el-table-column>
                <el-table-column prop="warehouseName" label="仓库名称" min-width="120" sortable show-overflow-tooltip>
                    <template #default="scope"> <span class="el-span-text" @click="opShow(scope.row, scope.$index)">{{scope.row.warehouseName}}</span></template>
                </el-table-column>
                <el-table-column prop="address" label="地址" min-width="160" sortable show-overflow-tooltip></el-table-column>
                <el-table-column prop="warehouseType" label="类型" min-width="120" sortable>
                    <template #default="scope">{{scope.row.warehouseTypeDesc}}</template>
                </el-table-column>
                <el-table-column prop="isExternal" label="外部托管仓" min-width="120" sortable>
                    <template #default="scope">{{scope.row.isExternalDesc}}</template>
                </el-table-column>
                <el-table-column prop="orgName" label="所属机构" min-width="120"  sortable></el-table-column>
                <el-table-column label="期初状态" fixed="right" align="center" min-width="110" sortable>
                    <template #default="scope">{{scope.row.isStockSet?"已完成":"未完成"}}</template>
                </el-table-column>
                <el-table-column label="开启负库存" fixed="right" align="center" min-width="120" sortable>
                    <template #default="scope">
                        <el-switch
                            v-model="scope.row.isMinus"
                            :active-value="true"
                            :inactive-value="false"
                            @change="setIsMinus(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="默认仓" fixed="right" align="center" min-width="90" sortable>
                    <template #default="scope">
                        <el-switch
                            v-model="scope.row.isDefault"
                            :active-value="true"
                            :inactive-value="false"
                            @change="setIsDefault(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="状态" fixed="right" align="center" min-width="85" sortable>
                    <template #default="scope">
                        <el-switch
                            v-model="scope.row.status"
                            :active-value="1"
                            :inactive-value="0"
                            @change="setStatus(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column prop="Remarks" label="备注" min-width="140" sortable></el-table-column>
                <el-table-column label="操作" fixed="right" align="center" min-width="230">
                    <template #default="scope">
                        <el-button-group>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="opShow(scope.row, scope.$index)"
                            >查看</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                :disabled="!editState"
                                @click="opEdit(scope.row, scope.$index)"
                            >编辑</el-button>
                            <el-popconfirm
                                title="确定删除吗？"
                                @confirm="opDelete(scope.row, scope.$index)"
                            >
                                <template #reference>
                                    <el-button
                                        text
                                        type="primary"
                                        size="small"
                                        :disabled="!deleteState"
                                    >删除</el-button>
                                </template>
                            </el-popconfirm>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                v-show="!scope.row.isStockSet"
                                @click="opStartStock(scope.row, scope.$index)"
                            >设置期初</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>

    <save-dialog
        v-if="dialogSave"
        ref="saveDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogSave = false"
    ></save-dialog>

    <start-stock-dialog
        v-if="dialogStartStock"
        ref="startStockDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogStartStock = false"
    ></start-stock-dialog>
</template>

<script>
import saveDialog from "./save";
import startStockDialog from "./startStock";
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
import config from "@/config";
export default {
    name: "warehouse",
    components: {
        saveDialog,
        startStockDialog,
        publicjs,
    },
    data() {
        return {
            dialogSave: false,
            dialogStartStock: false,
            showCondition: false,
            selectionRows: [],
            pkList: ["warehouseId"],
            columnList: [
            ],
            OrderList: { warehouseId: "Asc" },
            queryList: {
                warehouseCode: "",
                warehouseName: "",
                status: "",
            },
            result: "", // 运行结果
            props1: {
                checkStrictly: true,
                emitPath: false,
            },
            orgCodeList: "",
            orgNameList: "",
            addState: true, //可以增加
            editState: true, //可以修改
            editBatchState: true, //可以批量修改
            deleteState: true, //可以删除
            optionsState: [],
            optionsWarehouseType:[]
        };
    },
    methods: {
        //添加
        opAdd() {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Add");
            });
        },
        //编辑
        opEdit(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Edit").setData(row);
            });
        },
        //查看
        opShow(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Show").setData(row);
            });
        },
        //库存设置
        opStartStock(row) {
            this.dialogStartStock = true;
            this.$nextTick(() => {
                this.$refs.startStockDialog.open("Save").setData(row);
            });
        },
        //设置状态
        async setStatus(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    row.status == 0 ? "warehouse/disabled" : "warehouse/enable",
                    { warehouseIds: [row.warehouseId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //设置状态
        async setIsMinus(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    row.isMinus == 0 ? "warehouse/close" : "warehouse/open",
                    { warehouseIds: [row.warehouseId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //设置状态
        async setIsDefault(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "warehouse/default",
                    {
                        warehouseId: row.warehouseId,
                        warehouseType: row.warehouseType,
                        isDefault: row.isDefault == 1,
                    }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //删除
        async opDelete(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "warehouse/delete",
                    { warehouseIds: [row.warehouseId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //批量删除
        async opBatchDelete() {
            this.$confirm(
                `确定删除选中的 ${this.selectionRows.length} 项吗？`,
                "提示",
                {
                    type: "warning",
                }
            )
                .then(async () => {
                    const warehouseIds = this.selectionRows.map(
                        (p) => p.warehouseId
                    );
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                "warehouse/delete",
                                { warehouseIds: [...warehouseIds] }
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$refs.mytable.getData();
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                        }
                    } catch (error) {
                        this.loading = false;
                        return false;
                    }
                })
                .catch(() => {});
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selectionRows = selection;
        },
        //本地更新数据
        handleSaveSuccess(data, mode) {
            if (mode == "Add") {
                this.$refs.mytable.refresh();
            } else if (mode == "Edit") {
                this.$refs.mytable.refresh();
            }
        },
        async opBindData() {
            this.$refs.mytable.clearSort();
            this.$refs.mytable.getData();
        },
        resetSearch() {
            this.queryList = {};
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["Status","WarehouseType"] }
                );
                if (returnValue.status == 200) {
                    this.optionsState = returnValue.response.find(
                        (p) => p.code == "Status"
                    ).optionsDetails;
                    this.optionsWarehouseType = returnValue.response.find(
                        (p) => p.code == "WarehouseType"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
    },
    async mounted() {
        await this.initOptions();
        await this.opBindData();
    },
};
</script>
